﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using App1AppHandler;
using System.IO;
using System.Xml;
using Reborn_WorldServer.App1AppHandler;
using Reborn_WorldServer;

namespace Reborn_Server.App1AppHandler
{
    public class LearnRecipe : IConsumable
    {

        private Recipie associatedRecipie = null;

        public bool MeetsConsumeRequirements(Character character, Loot lootitem)
        {
            if (lootitem.XmlData == "")
            {
                character.SendMessage("Error: Item is not a recipie. Please bug report", "server");
                character.PlaySound("Ding", character.MapX, character.MapY);
                return false;
            }

            int recipieId = 0;

            StringReader sr = new StringReader(lootitem.XmlData);
            XmlTextReader xmlReader = new XmlTextReader(sr);
            while (xmlReader.Read())
            {
                if (xmlReader.Name == "xmldata" && xmlReader.IsStartElement())
                {
                    string recipieAttribId = xmlReader.GetAttribute("recipieid");
                    if (recipieAttribId != "")
                        recipieId = int.Parse(recipieAttribId);
                }
            }

            if (recipieId == 0)
            {
                character.SendMessage("Error: Item is not a recipie. Please bug report", "server");
                character.PlaySound("Ding", character.MapX, character.MapY);
                return false;
            }

            associatedRecipie = Managers.GameHandler.Recipies[recipieId];

            int characterSkill = System.Convert.ToInt32(Math.Floor(character.skillManager.GetCurrentLevel(associatedRecipie.Skill)));
            if (characterSkill == 0)
            {
                character.SendMessage("Your " + associatedRecipie.Skill + " skill is not high enough to learn this recipe!", "server");
                character.PlaySound("Ding", character.MapX, character.MapY);
                return false;
            }

            int levelDiff = associatedRecipie.ReqLevel - characterSkill;

            if (levelDiff > 2)
            {
                character.SendMessage("Your " + associatedRecipie.Skill + " skill is not high enough to learn this recipe!", "server");
                character.PlaySound("Ding", character.MapX, character.MapY);
                return false;
            }

            if (character.recipieManager.hasRecipie(associatedRecipie) == true)
            {
                character.SendMessage("You already know that recipe!", "server");
                character.PlaySound("Ding", character.MapX, character.MapY);
                return false;
            }

            return true;
        }

        public void Consume(Character myCharacter, Loot lootitem)
        {
            if (associatedRecipie != null)
            {
                myCharacter.recipieManager.addRecipie(associatedRecipie);
                myCharacter.SendMessage("You learn " + associatedRecipie.Name + "!", "server");
            }
        }
    }
}
